package de.ralf.urwalking.soplugin;

import java.util.ArrayList;
import java.util.HashMap;

import com.threed.jpct.World;

import de.rj.urwalking.config.Config;
import de.rj.urwalking.data.Building;
import de.rj.urwalking.data.SalientObject;
import de.rj.urwalking.db.DBSalientObject;
import de.rj.urwalking.util.Util;
import android.util.Log;

/**
 * This class is used to remove salient objects from the 3D-world
 * 
 * @author Florian Ludwig
 * @author Andreas Rauscher
 * @version 1.0 - September 30, 2013.
 */
public class SOHelper {

	/** this is used as TAG for the logs */
	private static final String TAG = Config.TAG;
	/** this flag activates the debug-mode which in turn activates the logs */
	private static final boolean DEBUG = Config.DEBUG;

	/** this ArrayList contains all ids of the salient objects which are going to be removed */
	private ArrayList<Integer> mSOsToRemove;
	/** this flag defines if there are objects to remove or not */
	private boolean removeSOsNow = false;

	/** empty constructor of the class (debug-mode is off) */
	public SOHelper() {

	}

	/**
	 * this method removes the salient objects with the ids of the earlier defined ArrayList from the world
	 * 
	 * @param world
	 *            world where the salient objects are removed from
	 */
	public void removeSalientObjectsFromWorld(World world) {
		if (removeSOsNow) {

			if (DEBUG) Log.d(TAG, "removeSalientObjectsFromWorld: mSOsToRemove: " + Util.join(", ", mSOsToRemove));

			for (int id : mSOsToRemove) {
				try {
					world.removeObject(id);
				} catch (Exception e) {
					if (DEBUG) Log.d(TAG, "Exception: " + e);
				}

			}

			mSOsToRemove = null;
			removeSOsNow = false;
		}
	}

	/**
	 * this method clears the delivered HashMap and the delivered ArrayList and saves all ids of the existing salient
	 * objects to a seperate ArrayList so that these objects can be removed from the world
	 * 
	 * @param world
	 *            is used to check if the world isn't equal to null
	 * @param dbSalientObjects
	 *            this HashMap is cleared to prevent errors while adding new salient objects
	 * @param existingLandmarks
	 *            this ArrayList is the source of the ids which are used to remove the respective salient objects from
	 *            the world later and is cleared afterwards
	 */
	public void removeSalientObjects(World world, HashMap<Integer, DBSalientObject> dbSalientObjects,
			ArrayList<SalientObject> existingLandmarks) {
		dbSalientObjects.clear();

		if (world != null && existingLandmarks.size() >= 0) {
			mSOsToRemove = new ArrayList<Integer>();

			for (SalientObject salientObject : existingLandmarks) {

				if (DEBUG)
					Log.d(TAG, "SalientObject: " + salientObject.getName() + " (ID: " + salientObject.getID() + ")");

				// we don't want to remove buildings, just remove the highlight that identifies them as SOs
				if (salientObject instanceof Building) {
					salientObject.dontShowAsLandmark();
				} else {
					mSOsToRemove.add(salientObject.getID());
				}

			}

			existingLandmarks.clear();
			removeSOsNow = true;
		}
	}

}